package org.pentaho.kettle.plugin.mqtt.steps.consumer;

import java.util.List;
import java.util.UUID;

import org.pentaho.di.core.CheckResult;
import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.annotations.Step;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.exception.KettleValueException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.pentaho.kettle.plugin.mqtt.DialogClassUtil;
import org.pentaho.metastore.api.IMetaStore;
import org.w3c.dom.Node;

@Step(
        id = "mqttConsumer",
        image = "org/pentaho/kettle/plugin/mqtt/steps/consumer/consumer.svg",
        i18nPackageName = "org.pentaho.kettle.plugin.mqtt.steps.consumer",
        name = "mqttConsumer.Name",
        description = "mqttConsumer.TooltipDesc",
        categoryDescription = "i18n:org.pentaho.di.trans.step:BaseStep.Category.Transform"
)
public class ConsumerStepMeta extends BaseStepMeta implements StepMetaInterface {
    private static Class<?> PKG = ConsumerStepMeta.class;
    private String outputField;
    private String subBroker;
    private String subTopic;
    private String clientId;
    private String username;
    private String password;
    private int qos;
    public static final String DIALOG_NAME;

    public ConsumerStepMeta() throws Throwable {
    }

    public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int cnr, TransMeta transMeta, Trans disp) {
        return new ConsumerStep(stepMeta, stepDataInterface, cnr, transMeta, disp);
    }

    public StepDataInterface getStepData() {
        return new ConsumerStepData();
    }

    public void setDefault() {
        this.outputField = "message";
        this.subBroker = "tcp://127.0.0.1:1883";
        this.subTopic = "";
        this.clientId = UUID.randomUUID().toString();
        this.qos = 1;
    }

    public String getOutputField() {
        return this.outputField;
    }

    public String getSubBroker() {
        return this.subBroker;
    }

    public String getSubTopic() {
        return this.subTopic;
    }

    public String getClientId() {
        return this.clientId;
    }

    public int getQos() {
        return this.qos;
    }

    public String getUsername() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setOutputField(String outputField) {
        this.outputField = outputField;
    }

    public void setSubBroker(String subBroker) {
        this.subBroker = subBroker;
    }

    public void setSubTopic(String subTopic) {
        this.subTopic = subTopic;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public void setQos(int qos) {
        this.qos = qos;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Object clone() {
        Object retval = super.clone();
        return retval;
    }

    public String getXML() throws KettleValueException {
        StringBuffer retval = new StringBuffer(300);
        retval.append("    ").append(XMLHandler.addTagValue("outputField", this.getOutputField()));
        retval.append("    ").append(XMLHandler.addTagValue("subBroker", this.getSubBroker()));
        retval.append("    ").append(XMLHandler.addTagValue("subTopic", this.getSubTopic()));
        retval.append("    ").append(XMLHandler.addTagValue("clientId", this.getClientId()));
        retval.append("    ").append(XMLHandler.addTagValue("qos", this.getQos()));
        retval.append("    ").append(XMLHandler.addTagValue("username", this.getUsername()));
        retval.append("    ").append(XMLHandler.addTagValue("password", this.getPassword()));
        return retval.toString();
    }

    public void loadXML(Node stepnode, List<DatabaseMeta> databases, IMetaStore metaStore) throws KettleXMLException {
        try {
            this.setOutputField(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "outputField")));
            this.setSubBroker(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "subBroker")));
            this.setSubTopic(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "subTopic")));
            this.setClientId(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "clientId")));
            this.setQos(Integer.parseInt(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "qos"))));
            //this.username = XMLHandler.getTagValue(stepnode, "USERNAME");
            //this.password = XMLHandler.getTagValue(stepnode, "PASSWORD");
            this.setUsername(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "username")));
            this.setPassword(XMLHandler.getNodeValue(XMLHandler.getSubNode(stepnode, "password")));
        } catch (Exception var5) {
            throw new KettleXMLException("Demo plugin unable to read step info from XML node", var5);
        }
    }

    public void saveRep(Repository rep, IMetaStore metaStore, ObjectId id_transformation, ObjectId id_step) throws KettleException {
        try {
            rep.saveStepAttribute(id_transformation, id_step, "outputField", this.outputField);
            rep.saveStepAttribute(id_transformation, id_step, "subBroker", this.subBroker);
            rep.saveStepAttribute(id_transformation, id_step, "subTopic", this.subTopic);
            rep.saveStepAttribute(id_transformation, id_step, "clientId", this.clientId);
            rep.saveStepAttribute(id_transformation, id_step, "qos", (long)this.qos);
            rep.saveStepAttribute(id_transformation, id_step, "username", this.username);
            rep.saveStepAttribute(id_transformation, id_step, "password", this.password);
        } catch (Exception var6) {
            throw new KettleException("Unable to save step into repository: " + id_step, var6);
        }
    }

    public void readRep(Repository rep, IMetaStore metaStore, ObjectId id_step, List<DatabaseMeta> databases) throws KettleException {
        try {
            this.outputField = rep.getStepAttributeString(id_step, "outputField");
            this.subBroker = rep.getStepAttributeString(id_step, "subBroker");
            this.subTopic = rep.getStepAttributeString(id_step, "subTopic");
            this.clientId = rep.getStepAttributeString(id_step, "clientId");
            this.qos = (int)rep.getStepAttributeInteger(id_step, "qos");
            this.username = rep.getStepAttributeString(id_step, "username");
            this.password = rep.getStepAttributeString(id_step, "password");
        } catch (Exception var6) {
            throw new KettleException("Unable to load step from repository", var6);
        }
    }

    public void getFields(RowMetaInterface inputRowMeta, String name, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space, Repository repository, IMetaStore metaStore) throws KettleStepException {
        ValueMetaInterface v = new ValueMeta(this.outputField, 2);
        v.setTrimType(3);
        v.setOrigin(name);
        inputRowMeta.addValueMeta(v);
    }

    public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, VariableSpace space, Repository repository, IMetaStore metaStore) {
        CheckResult cr;
        if (input.length > 0) {
            cr = new CheckResult(1, BaseMessages.getString(PKG, "Demo.CheckResult.ReceivingRows.OK", new String[0]), stepMeta);
            remarks.add(cr);
        } else {
            cr = new CheckResult(4, BaseMessages.getString(PKG, "Demo.CheckResult.ReceivingRows.ERROR", new String[0]), stepMeta);
            remarks.add(cr);
        }

    }

    public String getDialogClassName() {
        return DIALOG_NAME;
    }

    static {
        DIALOG_NAME = DialogClassUtil.getDialogClassName(PKG);
    }
}
